package
{
    public class BackgroundCreator2
    {
        import flash.display.*;
        import flash.geom.*;
        import flash.filters.*;

        import flash.display.Bitmap;
    
        
        public static function buildBackground(width:uint, height:uint):Bitmap
        {
            var data:BitmapData = new BitmapData(width, height,false, 0x000000); 
            var bitmap:Bitmap = new Bitmap(data);
            
            
            var sprite:Sprite = new Sprite();            
//            data.noise(500, 0, 10); 

              var matr:Matrix = new Matrix();
            matr.createGradientBox( 30, height*0.2, Math.PI /2, 0, height*0.8 );
            //SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals

            
            //sprite.graphics.beginFill(0x000000);
            sprite.graphics.beginGradientFill( GradientType.LINEAR, [ 0x00f0ff, 0xFFFFFF ], [ 1, 1 ], [ 0, 125 ], matr, SpreadMethod.PAD );            
            sprite.graphics.drawRect(0,0,width,height);
            sprite.graphics.endFill();
            
            for (var j:uint = 0; j<20; j++)
            {
                var x:uint = width*Math.random();
                var y:uint = height*Math.random();                        
                var array:Array = [];
                for (var i:uint = 0; i<10+Math.random()*10; i++) 
                {
                    x+=-50+Math.random()*100;
                    y+=-30+Math.random()*60;
                    
                    array.push([x,y,10+Math.random()*30]);
                    
                }

                for (var i=0; i<array.length; i++)
                {                
                    sprite.graphics.beginFill(0x818181);
                    sprite.graphics.drawCircle(array[i][0]+5,array[i][1]+5,array[i][2]);
                    sprite.graphics.endFill();
                }
                

                for (var i=0; i<array.length; i++)
                {                
                    sprite.graphics.beginFill(0xFFFFFF);
                    sprite.graphics.drawCircle(array[i][0],array[i][1],array[i][2]);
                    sprite.graphics.endFill();
                }

            }

                     
            
            sprite.filters = [new BlurFilter(5,5,BitmapFilterQuality.HIGH)];
         
            data.draw(sprite);
                        

            return bitmap;            
        }
        
        
      
            public static function interpolateColor(hex1:uint, hex2:uint, percent:Number):uint { 
                // percent is between 0 and 1 
                // 
                var alpha1:uint = hex1 >> 24 & 0xFF; 
                var red1:uint = hex1 >> 16 & 0xFF; 
                var green1:uint = hex1 >> 8 & 0xFF; 
                var blue1:uint = hex1 & 0xFF; 
                // 
                var alpha2:uint = hex2 >> 24 & 0xFF; 
                var red2:uint = hex2 >> 16 & 0xFF; 
                var green2:uint = hex2 >> 8 & 0xFF; 
                var blue2:uint = hex2 & 0xFF; 
                // 
                var alpha3:uint = alpha1+((alpha2-alpha1)*percent) 
                var red3:uint = red1+((red2-red1)*percent) 
                var green3:uint = green1+((green2-green1)*percent) 
                var blue3:uint = blue1+((blue2-blue1)*percent) 
                // 
                var a:String = alpha3.toString(16); 
                if (a.length==1){ 
                    a = "0"+a 
                } 
                var r:String=red3.toString(16); 
                if (r.length==1){ 
                    r = "0"+r 
                } 
                var g:String=green3.toString(16); 
                if (g.length==1){ 
                    g = "0"+g 
                } 
                var b:String=blue3.toString(16); 
                if (b.length==1){ 
                    b = "0"+b 
                } 
                // 
                var combined:String = '0x'+a+r+g+b 
                // 
                var hex3:uint = Number(combined) 
                // 
                return(hex3) 
                // 
            }; 
        
    }
}
